package com.kcn.upload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.commons.io.IOUtils;
import com.kcn.utils.PropertiesReader;
import com.kcn.xml.Doc2XML;
import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// 必须的参数，参数名与表单名相同，如果表单名为多个上传文件，则必须为数组或List
	private File[] upload;
	// 必须的参数，格式：表单名+ContentType，表示上传文件类型
	private String[] uploadContentType;
	// 必须的参数，格式：表单名+FileName，表示上传的文件名
	private String[] uploadFileName;
	// 文件保存的位置
	private String savePath;
	Doc2XML d2x = new Doc2XML();

	public String upload() throws Exception {
		String uploadPath = PropertiesReader.getPropertiesReader().getProperty(
				"uploadPath");
		System.out.println("upload path: " + uploadPath);
		System.out.println(this.getUploadFileName().length);
		System.out.println(this.getUpload().length);
		System.out.println("aaaabbbbb");
		for (int i = 0; i < this.getUpload().length; i++) {
			FileInputStream in = new FileInputStream(this.getUpload()[i]);
			// System.out.println(this.getSavePath() + "\\"
			// + this.getUploadFileName()[i]);
			FileOutputStream out = new FileOutputStream(uploadPath + "\\"
					+ this.getUploadFileName()[i]);
			// 将数据拷贝到上传目录，这里没有必要使用缓冲流，因为IOUtiles工具类已经使用了缓存
			IOUtils.copy(in, out);
			out.close();
			in.close();
			int recs=convertFormat(this.getUploadFileName()[i]);
			addActionMessage("转换记录总数"+recs);
			System.out.println("文件名称： " + this.getUploadFileName()[i]);
		}
		return SUCCESS;
	}

	// 调用转换格式
	public int convertFormat(String filename) {
		int recs=0;
		try {
			System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbbbbb");
			 recs=d2x.BuildXMLDOC(filename);
			System.out.println("ccccccccccccccccccccccccccc");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return recs;
	}

	public File[] getUpload() {
		return upload;
	}

	public void setUpload(File[] upload) {
		this.upload = upload;
	}

	public String[] getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String[] uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String[] getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

}
